<?php
//+----------------------------------------------------------------------
//| ThinkPHP [ WE CAN DO IT JUST THINK IT ]
//+----------------------------------------------------------------------
//| Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
//+----------------------------------------------------------------------
//| Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
//+----------------------------------------------------------------------
//| Author: liu21st <liu21st@gmail.com>
//+----------------------------------------------------------------------
/**
 * 日志处理类
 * @category   Think
 * @package  Think
 * @subpackage  Core
 * @author    liu21st <liu21st@gmail.com>
 * @version   $Id: Log.class.php 2791 2012-02-29 10:08:57Z liu21st $
 * @last modify 2012-12-04 16:29:13 by mashanling
 */
class Log {

    //日志记录方式
    const SYSTEM = 0;
    const MAIL   = 1;
    const FILE   = 3;
    const SAPI   = 4;

    static $log    = array();//日志信息
    static $format = '[ Y-m-d H:i:s ]';//日期格式

    /**
     * 记录日志 并且会过滤未经设置的级别
     *
     * @last modify 2012-12-04 16:32:01 by mashanling
     *
     * @access static,public
     *
     * @param string $message 日志信息
     *
     * @return void 无返回值
     */
    static public function record($message) {
        self::$log[] = $message;
    }

    /**
     * 保存记录日志
     *
     * @last modify 2012-12-04 16:32:22 by mashanling
     *
     * @access static,public
     *
     * @return void 无返回值
     */
    static public function save() {

        if (self::$log) {
            self::write();
            self::$log = array();
        }
    }

    /**
     * 写日志
     *
     * @last modify 2012-12-04 16:32:22 by mashanling
     *
     * @access static,public
     *
     * @param string $message       日志信息
     * @param int    $type          日志记录方式
     * @param string $destination   写入目标
     * @param string $extra         额外参数
     *
     * @return void
     */
    static public function write($message = '', $type = '', $destination = '', $extra = '') {
        $log  = $message ? $message : join(EOL_LF, self::$log);

        if (!$log) {
            return;
        }

        $log .= EOL_LF;
        $now  = local_date(self::$format);
        $type = $type ? $type : C('LOG_TYPE');

        if (self::FILE == $type) {//文件方式记录日志
            $destination = $destination ? $destination : LOG_PATH . local_date('Y-m-d') . '.log';

            //检测日志文件大小，超过配置大小则备份日志文件重新生成
            if (is_file($destination) && ($filesize = sys_config('sys_log_filesize')) && $filesize * 1024 <= filesize($destination)) {
                rename($destination, LOG_PATH . local_date('Y-m-d_His') . '.log');
            }
        }
        else {
            $destination = $destination ? $destination : C('LOG_DEST');
            $extra = $extra ? $extra : C('LOG_EXTRA');
        }

        if (APP_DEBUG && strpos($log, '] PHP ')) {
            send_http_status(500);
            echo nl2br($log);
        }

        error_log($log, $type, $destination, $extra);
    }
}